
#include "stdio.h"
#include "string.h"
#include "gd32f10x.h"
#include "systick.h"
#include "DRV_LED.h"
#include "DRV_UART.h"
#include "DRV_IRDA.h"
#include "DRV_Temp.h"
#include "DRV_Timer.h"
#include "DRV_WS2812B.h"
#include "DRV_Light_adc.h"

#include "pikaScript.h"


float TempData=0,HMData=0;

extern void msc_ram_init(void);

char __platform_getchar(void) 
{
    uint8_t ch;
    while(1)
    {
        if(RESET != usart_flag_get(DEBUG_UART, USART_FLAG_RBNE))
        {
            ch = (unsigned char)(usart_data_receive(DEBUG_UART));
            usart_interrupt_flag_clear(DEBUG_UART, USART_INT_FLAG_RBNE);
            return ch;
        }
    }
}

int main(void)
{
    unsigned char Flag=0;
    char buff[32];
    
    SysTick_Config(SystemCoreClock / 1000U);
    LED_IO_Init();    
    Uart0_Init(115200);
    WS2812_Init();
    IRDA_Init();
    Light_ADC_Init();
    SHTC3_Init();
    msc_ram_init();
    printf("hello\r\n");
    
    PikaObj *pikaMain = pikaScriptInit();
 	
    
    while(1)
    {
//        pikaScriptShell(pikaMain);
        
        for(int i=0;i<12;i++)
        {
            WS2812_PixelSetIndexColor(i,WS2812_RED);
            WS2812_SendData();
            HAL_Delay(100);
        }
        for(int i=0;i<12;i++)
        {
            WS2812_PixelSetIndexColor(i,WS2812_GREEN);
            WS2812_SendData();
            HAL_Delay(100);
        }
        for(int i=0;i<12;i++)
        {
            WS2812_PixelSetIndexColor(i,WS2812_BLUE);
            WS2812_SendData();
            HAL_Delay(100);
        }
        WS2812_ClearData();
        WS2812_SendData();
        
        
        
        if(IRDA_GetCode(&Flag)  == 1)
        {
            printf("IRDA_Data:0x%02X\r\n",Flag);
        }
        printf("Light_Data:%u.\r\n",Light_ADC_GetValue());
        
        Flag=SHTC3_GetTempAndHumi(&TempData,&HMData);
        if(Flag!=NO_ERROR)
        {
            if(Flag==0x01)
            {
                printf("Error:ACK_ERROR\r\n");
            }
            else if(Flag==0x02)
            {
                printf("Error:CHECKSUM_ERROR\r\n");
            }
        }
        else
        {
            printf("TEMP_Data:%0.6f\r\n",TempData);
            printf("HUMI_Data:%0.6f\r\n",HMData);
        }
    }
}




